<?php
/*
* 主要功能:用户的其本函数
*
* dreamxyp(QQ:31996798) - PHP - class.u.php
* coding:夏一平
* 创建时间:2006-10-10
*/
class u
{

	/*
	* 功能:登录初始化
	* 参数:无
	*/
	static public function login($urs,$pwd)
	{
		global $db_member;
		if(strlen($urs)>=4 && strlen($urs)<=20)
		{
			$rs = $db_member->rsArray("select * from member_main where `urs` = ? ",$urs);
			if($rs) 
			{
				$sha=sha1($pwd);
				$pwd=md5($pwd);
				if($sha == $rs['sha'] && $pwd == $rs['pwd'])
				{
					//cookie
					$pass_hex  = substr(sha1($sha.time()),10,13);
					$login_hex = md5(substr(base64_encode($urs . SITE_LOGINKEY . $pass_hex), strlen(SITE_LOGINKEY)%5, -1));
					//日志
					$time = time();
					$ip   = g::ip();
					$rs_log = $db_member->rsArray("select this_ip,this_time,out_time,online,online_time from member_log where `urs` = ? ",$urs);
					if(empty($rs_log))
					{
					 	$rs_log = array('urs'=>$urs, 'reg_ip'=>$ip,'reg_time'=>$time);
						$db_member->insert('member_log',$rs_log);
					}
					if($ip != $rs_log['this_ip'] && $rs_log['online'] && $time < $rs_log['out_time']+60*10)
					{
						return array(0,"提示:您正在别处的登录并未离线,请稍后再试!");
					}else
					{
						$login_times = ($ip == $rs_log['this_ip'] && $time < $rs_log['out_time']+60*15)?'':'login_times = login_times +1,';
						$online_time = max($rs_log['online_time']+($rs_log['out_time']-$rs_log['this_time']),$rs_log['online_time']);
						$db_member->conn("update member_log set last_ip=:last_ip,
																last_time=:last_time,
																this_ip=:this_ip,
																this_time =:this_time,
																out_time =:out_time,
																online = 1,
																{$login_times}
																online_time =:online_time where `urs` = :urs ",array(
																	'last_ip'=>$rs_log['this_ip'],
																	'last_time'=>$rs_log['this_time'],
																	'this_ip'=>$ip,
																	'this_time'=>$time,
																	'out_time'=>$time,
																	'online_time'=>online_time,
																	'urs'=>$urs
																));
						//echo $db_member->getsql();
						//exit();
						g::setcookie('urs',$urs,24);
						g::setcookie('pass_hex',$pass_hex,6);
						g::setcookie('login_hex',$login_hex,6);
						g::setcookie('out_time',$time,1);
						return array(1,"成功:登录成功!",$rs);
					}
				}else
				{
					return array(0,"提示:密码有误,请得重试!");
				}					
			}else
			{
				return array(0,"提示:会员名不存在!");
			}
		}else
		{
			return array(0,"提示:会员名格式不对!");
		}
	}
	static public function modifyPass($urs,$pwd,$new_pwd)
	{
	    global $db_member;
	    $rs = $db_member->rsArray("select * from member_main where `urs` = ? ",$urs);
		if($rs) 
		{
			$sha=sha1($pwd);
			$pwd=md5($pwd);
			if($sha == $rs['sha'] && $pwd == $rs['pwd'])
			{
			    
			    $bind = array(
					'pwd'=>md5($new_pwd),
					'sha'=>sha1($new_pwd)
				);
				$id = $db_member->update("member_main",$bind,"`urs` = ? ",$urs);
				if($id)
				{
				    return array(1,"成功:更改密码成功!");
				}else
				{
				    return array(0,"提示:系统忙,请稍后再试!");
				}
			}else
			{
			    return array(0,"提示:密码输入有误!");
			}
		}else
		{
		     return array(0,"提示:登录超时,或你还没有登录!");
		}		
	}
	static public function outLogin()
	{
		if(urs)
		{
			global $db_member;
			$db_member->update("member_log",array('online'=>0)," `urs` = ? ",urs);
		}
		g::setcookie('pass_hex','',-1);
		g::setcookie('login_hex','',-1);
		g::setcookie('out_time','',-1);
	}
	//更登录时间
	static public function times()
	{
		global $db_member;
		$time	 = time();
		$out_time = $_COOKIE['out_time'];
		if (urs && $out_time + 100 < $time)
		{
			$db_member->update('member_log',array('out_time'=>$time)," `urs` =? ",urs);
			g::setcookie('urs',urs,24);
			g::setcookie('pass_hex',$_COOKIE['pass_hex'],6);
			g::setcookie('login_hex',$_COOKIE['login_hex'],6);
			g::setcookie('out_time',$time,1);
		}
	}
	//用户注册
	static public function reg($urs,$pwd,$mail)
	{
		global $db_member;
		if(strlen($urs)>=4
			&& strlen($urs)<=20
			&& strlen($pwd)>=6
			&& strlen($pwd)<=16
			&& g::isemail($mail))
		{
			$table 		= 'member_main';
			$rs 		= $db_member->rowRepeat($table,'urs',$urs);
			if($rs)
			{
				return array(0,"提示:对不起会员名“{$urs}”已被注册！");
			}else
			{
				$rs 		= $db_member->rowRepeat($table,'mail',$mail);
				if($rs)
				{
					return array(0,"提示:对不起email“{$mail}”已被注册使用！");
				}else
				{
				    $bind = array(
						'urs'=>$urs,
						'mail'=>$mail,
						'pwd'=>md5($pwd),
						'sha'=>sha1($pwd)
					);
					$id = $db_member->insert($table,$bind);
					//echo $db_member->getsql();
					//exit();
					if($id)
					{
						//记下日志
						$ip	  = g::ip();
						$time = time();
						$bind = array(
							'urs'=>$urs,        
	                   		'reg_ip'=>$ip,
							'reg_time'=>$time
							);
						$db_member->insert('member_log',$bind);
						return array(1,"提示:注册成功！ ");
					}else
					{
						return array(0,"提示:系统忙请稍后重试！");
					}
				}
			}
		}else
		{
			return array(0,"提示:会员名“{$urs}”,密码“{$pwd}”或email“{$mail}”格式不正确！ ");
		}
	}
	/*
	 * 用户名检查
	 * */  
	static public function regChk($urs)
	{	
	  	global $db_member;
		if($urs)
		{
			if(strlen($urs)>=4
				&& strlen($urs)<=20)
			{
				$rs 		= $db_member->rowRepeat('member_main','urs',$urs);
				if($rs)
				{
					return array(0,"提示:对不起会员名“{$urs}”已被注册！");
				}else
				{
					return array(1,"“{$urs}”会员名还没被注册，可以注册！");
				}
			}else
			{
				return array(0,"提示:這裡只能是4-20个字符！");
			}
		}else
		{
			return array(0,"提示:会员名不能为空！");
		}
	}
}
?>